//
//  AddRemindViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/2.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "AddRemindViewController.h"
#import "MyInfoTableViewCell.h"
#import "DTPickerManager.h"
#import "SaveRemindApi.h"
#import "RemindModel.h"
@interface AddRemindViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,strong) UITextField *remindTextField;
//@property(nonatomic,copy) NSString *dateString;
//@property(nonatomic,copy) NSString *timeString;


@end

@implementation AddRemindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark --- 点击事件
- (void)onRightButtonClick:(id)sender
{
    if (!self.remindModel.date)
    {
        [SVProgressHUD showErrorWithStatus:@"请选择日期"];
        return;
    }
    
    if (!self.remindModel.time)
    {
        [SVProgressHUD showErrorWithStatus:@"请选择时间"];
        return;
    }
    
    if (self.remindTextField.text.length == 0)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入相应事项"];
        return;
    }
    
    [self saveRemind];
}

- (void)saveRemind
{
    NSString *string = [self.remindTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *remindTime = [self.remindModel getDateString];
    SaveRemindApi *api = [[SaveRemindApi alloc] initWithDic:@{@"sn":EXRACT_OBJECT(DEVICEMAC),@"remind_time":remindTime,@"content":string,@"id":self.remindModel.ids}];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showSuccessWithStatus:[request responseJSONObject][@"errmsg"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[DataManager sharedInstance] sendCode:@"1003" data:@{}];
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failure:nil];
}

#pragma mark --- tabelview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return px1920Height(150);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return px1920Height(30);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    MyInfoTableViewCell *cell = [MyInfoTableViewCell dequeueReusableWithTableView:tableView];
    if (indexPath.section == 0)
    {
        cell.titleLabel.text = @"日期";
        cell.infoLabel.text = self.remindModel.date;
    }
    else
    {
        cell.titleLabel.text = @"时间";
        cell.infoLabel.text = self.remindModel.time;
    }
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    switch (indexPath.section)
    {
        case 0:
        {
            [[DTPickerManager shareManager] showDate:[NSDate date] pickerWithFutureDateAndPicker:^(NSArray *arr) {
                
            } andConfirm:^(NSArray *arr) {
                if (arr.count == 3)
                {
                    NSString *string = [NSString stringWithFormat:@"%@年%@月%@日",arr[0],arr[1],arr[2]];
                    self.remindModel.date = string;
                    [self.tableView reloadData];
                }
            }];
        }
            break;
        case 1:
        {
            [[DTPickerManager shareManager] showDate:[NSDate date] pickerWithTimeAndPicker:^(NSArray *arr) {
                
            } andConfirm:^(NSArray *arr) {
                if (arr.count == 2)
                {
                    self.remindModel.time = [NSString stringWithFormat:@"%@:%@",arr[0],arr[1]];
                    [self.tableView reloadData];
                }
            }];
        }
            break;
            
        default:
            break;
    }
    
    
}

#pragma mark --- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [superView addSubview:_tableView];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}


- (UITextField *)remindTextField
{
    if(!_remindTextField)
    {
        UIView *superView = self.view;
        _remindTextField = [[UITextField alloc]init];
        [superView addSubview:_remindTextField];
        _remindTextField.placeholder = @"请输入相应事项";
        _remindTextField.font = fontCommonSize;
        _remindTextField.textColor = [UIColor whiteColor];
        [_remindTextField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];

    }
    return _remindTextField;
}


#pragma mark --- 设置视图布局
- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(252));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.height.mas_equalTo(px1920Height(330));
    }];
    
    UILabel *tipsLabel = [[UILabel alloc] init];
    [superView addSubview:tipsLabel];
    tipsLabel.text = @"事项";
    tipsLabel.textColor = [UIColor whiteColor];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.tableView.mas_left);
        make.top.mas_equalTo(self.tableView.mas_bottom).with.offset(px1920Height(100));
    }];
    
    [self.remindTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tipsLabel.mas_bottom).with.offset(px1920Height(52));
        make.left.right.mas_equalTo(self.tableView);
    }];
    
    UIView *lineView = ({
        lineView = [[UIView alloc]init];
        [superView addSubview:lineView];
        lineView.backgroundColor = rgba(27,44,81,1);
        [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.remindTextField.mas_bottom).with.offset(px1920Height(30));
            make.left.right.mas_equalTo(self.tableView);
            make.height.mas_equalTo(1);
        }];
        lineView;
        
    });
    
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"添加提醒"];
    [self setRightButtonTitle:@"完成" titleColor:rgba(143,157,189,1) titleFont:fontWithSize(14)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
